<template>
  <view>
    <button @click="submit">生成表格</button>
    <button @click="submit1">添加单位</button>
    <button @click="submit2">商品查询</button>
    <button @click="submit3">商户池</button>
  </view>
</template>

<script setup>
	import { exportToExcel, arrText } from '@/utils/ty.js'
	import { roleMenuTreeselect, menuAll } from "@/api/system/menu"
	import { getRoles } from "@/api/system/role"
	import useUserStore from '@/store/modules/user'
	import useSetStore from '@/store/modules/set'
	import { onLoad } from '@dcloudio/uni-app';
	import { editData } from "@/api/system/dict/data"
	import { computed, onMounted, ref } from 'vue';
	import { productInfo } from "@/api/erp_api/product"
	import { supplierProduct } from "@/api/erp_api/purchase"
	import { salesmanClue } from "@/api/erp_api/sale"
	import { salesmanClueStatusList, salesmanClueIsPublicList } from '@/utils/config.js'
	const userStore = useUserStore()
	const setStore = useSetStore()
	
	onLoad(async () => {
		await getRolesFun()
		await roleMenuTreeselectFun()
		await menuAllFun()
	})
	const roleList = ref([])
	const getRolesFun = () => {
		getRoles().then(res => {
			roleList.value = res.rows
		})
	}
	const roleMenuList = ref([])
	const roleMenuTreeselectFun = () => {
		roleMenuTreeselect(1).then(res => {
			roleMenuList.value = res.menus
		})
	}
	const menuList = ref([])
	const menuAllFun = () => {
		menuAll().then(res => {
			menuList.value = res.data
		})
	}
	
	const flattenTree = (arr, level = 1) => {
	  return arr.reduce((result, item) => {
			const { children, ...rest } = item;
			result.push({ ...rest, level }); // 添加当前层级
			if (children) result.push(...flattenTree(children, level + 1));
			return result;
		}, [])
	}
	
	const isHidden = (item) => {
		if (item.parentId) {
			const parentMenu = menuList.value.filter(item1 => item1.menuId === item.parentId)
			if (parentMenu[0]?.visible == 1) {
				return true
			} else {
				isHidden(parentMenu[0])
			}
		} else {
			return false
		}
	}
	
	const getData = (data) => {
		const keyArr = roleList.value.sort((a, b) => parseInt(a.roleSort) - parseInt(b.roleSort)).map(item => item.roleName)
		const menuData = menuList.value.filter(item => item.menuId === data.id)
		let obj = {
			'一级权限': data.level === 1 ? data.label || '' : '',
			'二级权限': data.level === 2 ? data.label || '' : '',
			'三级权限': data.level === 3 ? data.label || '' : '',
			'四级权限': data.level === 4 ? data.label || '' : '',
			'权限标识': menuData[0]?.perms || '',
			'页面': '',
		}
		keyArr.forEach((value, index) => {
			obj[value] = ''
		})
		return obj
	}
	
	const submit = () => {
		// console.log(roleList.value)
		// console.log(menuList.value)
		// console.log(roleMenuList.value)
		// 示例数据
		const sampleData = []
		const list = flattenTree(roleMenuList.value)
		for(let i = 0; i < list.length; i++) {
			sampleData.push(getData(list[i]))
		}
		console.log(sampleData)
		// 调用函数导出Excel
		exportToExcel(sampleData, '权限统计表.xlsx')
	}
	
	
	
	
	
	
	
	
	
	
	const setList = (arr, start1, start2) => {
		let newArr = []
		let dictValue = start1
		let dictSort = start2
		for (let i = 0; i < arr.length; i++) {
			newArr.push({
				dictLabel: arr[i],
				dictValue: dictValue,
				dictSort: dictSort,
				dictType: 'zyz_product_unit',
				status: 0
			})
			dictValue++
			dictSort++
		}
		return newArr
	}
	
	const submit1 = async () => {
		const unit = [
			// '1件(20袋*10片)',
			// '1件(30袋*360g)',
			// '1件(8袋*26片*90g)',
			// '1件(120g*100片)',
			// '1件(6袋*2.5kg)',
			// '1件(12袋*1.5kg)',
			// '1件(75袋*140g)',
			// '1件(15包*30支)',
			// '1件(8包*1.5kg)',
			// '1件(4袋*25支)',
			// '1件(4袋*40支)'
		]
		const list = setList(unit, 383, 1237)
		for(let i = 0; i < list.length; i++) {
			const result = await addOne({
				...list[i]
			})
			if (result) {
				console.log(`${i}:${list[i].dictLabel}创建成功`)
			} else {
				console.error(`${i}:${list[i].dictLabel}创建失败`)
			}
		}
	}
	const addOne = (params) => {
		return new Promise((resolve, reject) => {
			editData(params).then(res => {
				resolve(true)
			}).catch(err => {
				resolve(false)
			})
		})
	}
	
	const submit2 = async () => {
		// 未加商品供应商
		const supplierProductList = await supplierProduct()
		const productInfoList = await productInfo({
			regionCode: 500000
		})
		const list1 = supplierProductList.rows
		const list2 = productInfoList.rows
		const list3 = []
		const list4 = []
		const list5 = []
		// const list7 = list2.filter(item => item.name.trim().indexOf(item.unit.trim()) === -1).map(item => {
		// 	return {
		// 		'ID': item.id,
		// 		'商品名称': item.name,
		// 		'商品单位': item.unit
		// 	}
		// })
		// console.log(list7)
		// 商品信息有供应商没有
		for(let i=0;i<list2.length;i++) {
			const filterArr = list1.filter(item => item.productId == list2[i].id)
			if (filterArr.length === 0) {
				list3.push({
					id: list2[i].id,
					'商品名称': list2[i].name
				})
			} else {
				if (filterArr[0].purchasePrice != list2[i].purchasePrice) {
					list4.push({
						id: list2[i].id,
						'商品名称': list2[i].name,
						'商品采购价格': list2[i].purchasePrice,
						'供应商商品采购价格': filterArr[0].purchasePrice
					})
				}
				if (filterArr[0].unit != list2[i].unit) {
					list5.push({
						id: list2[i].id,
						'商品名称': list2[i].name,
						'商品单位': list2[i].unit,
						'供应商商品单位': filterArr[0].unit
					})
				}
			}
			if (i===list2.length-1) {
				console.log(`供应商未加：`)
				console.log(list3)
				console.log(`供应商价格不对：`)
				console.log(list4)
				console.log(`供应商单位不对：`)
				console.log(list5)
			}
		}
		exportToExcel(list3, '供应商未加.xlsx')
		exportToExcel(list4, '供应商价格不对.xlsx')
		// exportToExcel(list5, '供应商单位不对.xlsx')
		// 供应商有商品信息没有
		const list6 = []
		for(let i=0;i<list1.length;i++) {
			const filterArr = list2.filter(item => item.id == list1[i].productId)
			if (filterArr.length === 0) {
				list6.push({
					id: list1[i].productId,
					'商品名称': list1[i].productName,
					'价格': list1[i].purchasePrice
				})
			}
			if (i===list1.length-1) {
				// console.log(list6)
			}
		}
		
		
		// productInfo().then(res => {
		// 	console.log(res.rows)
		// 	const list = res.rows
		// 	const list1 = list.filter(item => item.wholePrice <= item.purchasePrice)
		// 	console.log(list1)
		// })
		// 示例数据
		// const sampleData = []
		// const list = flattenTree(roleMenuList.value)
		// for(let i = 0; i < list.length; i++) {
		// 	sampleData.push(getData(list[i]))
		// }
		// console.log(sampleData)
		// // 调用函数导出Excel
		// exportToExcel(sampleData, '权限统计表.xlsx')
	}
	
	const submit3 = async () => {
		const data = await salesmanClue()
		const list = data.rows.map(item => {
			return {
				...item,
				phone1: item.phone.replace(/\s/g, "").slice(0, 11)
			}
		}).filter(item => item.phone != '18888888888')
		let list1 = []
		for(let i=0;i<list.length;i++) {
			const filter = list.filter(item => item.phone1 == list[i].phone1)
			if (filter.length > 1) {
				for (let j = 0; j < filter.length; j++) {
					if (!(list1.find(item => item.id === filter[j].id))) {
						list1.push(filter[j])
					}
				}
			}
			if (i===list.length-1) {
				console.log(`商户池重复：`)
				console.log(list1)
				exportToExcel(list1.map(item => {
					return {
						'ID': item.id,
						'区域': arrText(setStore.allProvinceList , item.regionCode),
						'客户姓名': item.customerName,
						'重复电话': item.phone1,
						'联系电话': item.phone,
						'门店名称': item.storeName,
						'门店地址': item.addr,
						'地址详情': item.addrDetail,
						'是否公海': arrText(salesmanClueIsPublicList, item.isPublic),
						'关联销售': item.relationSalesmanName,
						'拜访人': item.createName,
						'拜访时间': item.createTime,
						'线索状态': arrText(salesmanClueStatusList, item.status),
						'转化订单': item.translationOrderNumber
					}
				}), '商户池重复.xlsx')
			}
		}
		
	}
	
</script>

<style scoped>
  page {
    background-color: #ffffff;
  }

  .view-title {
    font-weight: bold;
  }

  .view-content {
    font-size: 26rpx;
    padding: 12px 5px 0;
    color: #333;
    line-height: 24px;
    font-weight: normal;
  }
</style>
